package com.estate.web.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estate.common.annotation.Log;
import com.estate.common.constant.HttpStatus;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.core.domain.entity.TransactionData;
import com.estate.common.core.page.TableDataInfo;
import com.estate.common.enums.BusinessType;
import com.estate.common.utils.PageOutPutBean;
import com.estate.common.utils.PageQuery;
import com.estate.system.service.ITransactionDataService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/data/import")
public class TransactionDataController {

    @Resource
    private ITransactionDataService transactionDataService;

    @ApiOperation(value = "导入销售数据，并返回（批次号：importId）")
    @PostMapping(value = "/sales")
    public AjaxResult importSalesData(@RequestParam(value = "file") MultipartFile file) {
        return transactionDataService.importSalesData(file);
    }

    @ApiOperation(value = "根据ImportId，查询导入数据")
    @PostMapping(value = "/pre-view/{importId}")
    public AjaxResult preViewImportSalesData(@PathVariable(value = "importId") String importId, @RequestBody PageQuery pageQuery) {
        PageOutPutBean<TransactionData> list = transactionDataService.preViewImportSalesData(importId, pageQuery);
        return AjaxResult.success(list);
    }

    @Log(title = "Data Process", businessType = BusinessType.INSERT)
    @ApiOperation(value = "根据ImportId，保存数据数据")
    @GetMapping(value = "/save-data/{importId}")
    public AjaxResult saveImportSalesData(@PathVariable(value = "importId") String importId) {
        return transactionDataService.saveImportSalesData(importId);
    }

    @ApiOperation(value = "获取最新导入信息（时间、记录总数）")
    @GetMapping(value = "/lasted/extra-info")
    public AjaxResult extraInfoOfSalesData() {
        return transactionDataService.extraInfoOfSalesData();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(Page<?> page) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("Query successful");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}
